window.onload=function()
{ 	
	getSlice(0)
	// fillIndex()
	huadong()


	window.onscroll=function()
	{
		if (window.innerWidth<1200)
		{
			var sl=-Math.max(document.body.scrollLeft,document.documentElement.scrollLeft)
			document.getElementById("head").style.left=sl-100+"px"
		}
		
	}
   
	window.onresize=function()
	{      
		    huadong()
	}

}

function huadong()
{ 

	 if(window.innerWidth<1018)
		    {
		    	document.getElementById("head").style.left=-100+"px"
		    }
		     else if(window.innerWidth>1018&&window.innerWidth<1200)
		     {
		     	document.getElementById("head").style.left=(window.innerWidth-1018)*0.5-100+"px"
		     }
		     else{
		     	document.getElementById("head").style.left=0+"px"
		     }

}

function toMusic()
{
    data={"index":"index"}
	var httpRequest=new XMLHttpRequest()
    httpRequest.open("POST","/toMusic",true)
    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
    httpRequest.onreadystatechange = function () 
    {
	    if (httpRequest.readyState == 4 && httpRequest.status == 200)
	    {
	        flag=httpRequest.responseText
	        data=JSON.parse(flag)["all"]
	        nameContainer=document.getElementById("nameContainer")
	        nameContainer.value='{"all":'+JSON.stringify(data)+'}'
	        getSlice(0)

	        allLength=$("#allLength").parentContainer[0]
		    allLength.innerText="共"+Math.ceil(data.length/30)+"页"
		    $("#yesNumber").parentContainer[0].value=1
	
	       
	     }
	    
	}
  	httpRequest.send(JSON.stringify(data)); //设

}




function fillIndex()
{
	obj=$(".number").parentContainer
	for(var i=0;i<obj.length;i++)
	{
		obj[i].innerHTML=(i*1+1)+"、"
	}
}



function autoPlay(e)
{  
	aud=document.getElementById("aud")
    sp=$(".title").parentContainer
	for(var h=0;h<sp.length;h++)
	{
		sp[h].style.color="#fff"
	}

	
    countPlayer=document.getElementById("countPlayer")
   
	musicName=$("#musicName").parentContainer[0]

	
	if(e.style.color.length==0||e.style.color=="black")
	{  
		var httpRequest=new XMLHttpRequest()
		countPlayer=document.getElementById("countPlayer")
		name=sp[countPlayer.value*1].innerText
		sp[0].style.color="red"
		
		data={"musicName":name}

	    httpRequest.open("POST","/getMusic",true)
	    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
	    httpRequest.onreadystatechange = function () 
	    {
		    if (httpRequest.readyState == 4 && httpRequest.status == 200)
		    {
		        flag=httpRequest.responseText
		        if(flag.indexOf("没有找到")!=-1)
		        {
		        	alert(flag)
		        }
		        else
		        {
					aud.setAttribute("src","/static/musics/musics/"+flag)
		        	yesNumber=$("#yesNumber").parentContainer[0]
					musicName.innerText=yesNumber.value+"页/"+sp[countPlayer.value*1].previousSibling.innerText+sp[countPlayer.value*1].innerText
					aud.addEventListener('ended',end,false);
					aud.addEventListener("error",er, false);
					e.style.color="red"
		        }
		    }
		}
	    httpRequest.send(JSON.stringify(data)); //设置为发送给服务器数据
	}
	else{
		aud.removeEventListener('ended',end,false);
		aud.removeEventListener("error",er, false);
		e.style.color="black"
		aud.setAttribute("src","#")
		countPlayer=document.getElementById("countPlayer")
		
	}







	function end(e)
	{
		au=document.getElementById("au")
    	if(au.style.color=="red")
    	{   

    		var httpRequest=new XMLHttpRequest()
    		countPlayer=document.getElementById("countPlayer")
    		addOne=countPlayer.value*1+1
			name=sp[addOne].innerText
			data={"musicName":name}

		    httpRequest.open("POST","/getMusic",true)
		    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
		    httpRequest.onreadystatechange = function () 
		    {
			    if (httpRequest.readyState == 4 && httpRequest.status == 200)
			    {
			        flag=httpRequest.responseText
			        if(flag.indexOf("没有找到")!=-1)
			        {
			        	alert(flag)
			        }
			        else
			        {   
			    		
					    url="static/musics/musics/"+flag
					    aud.setAttribute("src",url)
					    yesNumber=$("#yesNumber").parentContainer[0]
					    musicName.innerText=yesNumber.value+"页/"+sp[addOne].previousSibling.innerText+name
					    countPlayer.value=addOne
					    sp[addOne].style.color="red"
					    sp[addOne-1].style.color="#fff"
			        }
			    }
			}
		  httpRequest.send(JSON.stringify(data)); //设置为发送给服务器数据
    	
    	}
	}

    function er()
    {  
    	au=document.getElementById("au")
    	if(au.style.color=="red")
    	{  
    		countPlayer=document.getElementById("countPlayer")
    		addOne=countPlayer.value*1+1
    		name=sp[addOne].innerText
    		sp[addOne].style.color="red"
    		sp[addOne-1].style.color="#fff"
			data={"musicName":name}
			var httpRequest=new XMLHttpRequest()
		    httpRequest.open("POST","/getMusic",true)
		    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
		    httpRequest.onreadystatechange = function () 
		    {
			    if (httpRequest.readyState == 4 && httpRequest.status == 200)
			    {
			        flag=httpRequest.responseText
			        if(flag.indexOf("没有找到")!=-1)
			        {
			        	alert(flag)
			        }
			        else
			        {
			        	url="/static/musics/musics/"+flag
			        	aud.setAttribute("src",url)
			        	musicName=document.getElementById("musicName")
			        	yesNumber=$("#yesNumber").parentContainer[0]
			        	musicName.innerText=yesNumber.value+"页/"+sp[addOne].previousSibling.innerText+name
			        	countPlayer.value=addOne
			        }
			    }
			}
		  httpRequest.send(JSON.stringify(data)); //设置为发送给服务器数据
	    	
    	}
    }
}




function cli(e)
{
	audio=document.getElementById("aud")
	name=e.innerText
	url="static/musics/musics/"
	data={"musicName":name}
	allTitle=$(".title").parentContainer
	for(var h=0;h<allTitle.length;h++)
	{
		allTitle[h].style.color="#fff"
	}
	e.style.color="red"

	countPlayer=$("#countPlayer").parentContainer[0]
	var httpRequest=new XMLHttpRequest()
    httpRequest.open("POST","/getMusic",true)
    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
    httpRequest.onreadystatechange = function () 
    {
	    if (httpRequest.readyState == 4 && httpRequest.status == 200)
	    {
	        flag=httpRequest.responseText
	        if(flag.indexOf("没有找到")!=-1)
	        {
	        	alert(flag)
	        }
	        else
	        {
	        	url=url+flag
	        	audio.setAttribute("src",url)
	        	musicName=document.getElementById("musicName")
	        	yesNumber=$("#yesNumber").parentContainer[0]
	        	musicName.innerText=yesNumber.value+"页/"+e.previousSibling.innerText+name
	        	countPlayer=$("#countPlayer").parentContainer[0]
	        	countPlayer.value=e.previousSibling.innerText.replace("、","")*1-1
	        	audio.addEventListener('ended',function()
	        		{
	        			e.style.color="#fff"
	        		},false);
	        	
	        }
	    }
	}
  httpRequest.send(JSON.stringify(data)); //设置为发送给服务器数据

}
function showMore(e)
{
	color=e.style.color
	more=$("#more").parentContainer[0]
	if(color=="red")
	{
		more.style.display="none"
		e.style.color="#fff"
	}
	else{
		more.style.display="inline-block"
		e.style.color="red"
	}

}

function showChangeBox()
{
	show=document.getElementById("change")
	selectFile=document.getElementById("selectFile")
	e=document.getElementById("updata")
	sflag=e.innerText.replace(/\s*/ig,"")

    getData=$("#getData").parentContainer[0]

	changYes=$("#changYes").parentContainer[0]
    yes=$("#yes").parentContainer[0]
    subm=$("#subm").parentContainer[0]
    changYes.style.display="none"
    yes.style.display="inline-block"
    subm.style.display="inline-block"
    
	if(sflag=="功能")
	{ 
	   changeValue=$(".changeValue").parentContainer
	   changeValue[0].value=""	
	   changeValue[1].value=""	
	   changeValue[2].value=""	
	   show.style.display="block"
	   selectFile.style.display="block"
	   e.innerText="关闭"
	}
	else{
		show.style.display="none"
		selectFile.style.display="none"
	    e.innerText="功能"
	    getData.value="yes"
	}


}

function updataFile()
{
    file=document.getElementById("fileStream")
    changeValue=$(".changeValue").parentContainer
   

    selectFile=$("#selectFile").parentContainer[0]
    fileName=file.value.split("\\")
    updataFlag="no"
    var data = new FormData();
    if(selectFile.style.display!="none")
    {
    	data.append("fileStream", file.files[0]);
    	data.append("fileName",fileName[fileName.length-1])
    	updataFlag="yes"
    	//如果没有选中文件就提交就不进行上传
    	if(fileName.length==1)
	    {
	    	alert("请选择文件")
	    	return 
	    }
    }

    data.append("updataFlag",updataFlag)
    
    name=changeValue[0].value
    cls=changeValue[1].value
    pr=changeValue[2].value
    if(name.replace(/\s*/ig,"").length==0||cls.replace(/\s*/ig,"").length==0||pr.replace(/\s*/ig,"").length==0)
    {
    	alert("名称或者分类，或者描述为空")
    	return
    }

    oldName=$("#oldName").parentContainer[0].value
    oldCls=$("#oldCls").parentContainer[0].value
    oldPr=$("#oldPr").parentContainer[0].value

	data.append("name",name)
    data.append("cls",cls)
    data.append("pr",pr)

    data.append("oldName",oldName)
    data.append("oldCls",oldCls)
    data.append("oldPr",oldPr)

 	
	var httpRequest=new XMLHttpRequest()
    httpRequest.open("POST","/updataFile",true)
    httpRequest.onreadystatechange = function () 
    {
	    if (httpRequest.readyState == 4 && httpRequest.status == 200)
	    {
	        flag=httpRequest.responseText
	     
	        if(flag.indexOf("已经有名称")!=-1)
	        {
	        	alert(flag+56)
	        }
	        if(flag=="ok")
	        {
	        	$("#oldName").parentContainer[0].value=name
	        	$("#oldCls").parentContainer[0].value=cls
	        	$("#oldPr").parentContainer[0].value=pr
	        	showChangeBox()
	        	file.value=""
	        	toMusic()
	        	$("#change").css("display","none")
	        	$("#updata").parentContainer[0].innerText="功能"

	        }
	    }
	}

  httpRequest.send(data); //设置为发送给服务器数据
}


function search()
{
    changeValue=$(".changeValue").parentContainer
    name=changeValue[0].value
    cls=changeValue[1].value
    pr=changeValue[2].value
    if(name.replace(/\s*/ig,"").length==0&&cls.replace(/\s*/ig,"").length==0&&pr.replace(/\s*/ig,"").length==0)
    {
    	alert("名称和分类和描述不能同时为空")
    	return
    }
    data={"name":name,"cls":cls,"pr":pr}

	var httpRequest=new XMLHttpRequest()
    httpRequest.open("POST","/searchMusic",true)
    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
    httpRequest.onreadystatechange = function () 
    {
	    if (httpRequest.readyState == 4 && httpRequest.status == 200)
	    {
	        flag=httpRequest.responseText
	        data=JSON.parse(flag)["music"]

	        nameContainer=document.getElementById("nameContainer")
	        nameContainer.value='{"all":'+JSON.stringify(data)+'}'
	        getSlice(0)


	        allLength=$("#allLength").parentContainer[0]
		    allLength.innerText="共"+Math.ceil(data.length/30)+"页"
	        showChangeBox()
	         $("#yesNumber").parentContainer[0].value=1
	         $("#change").css("display","none")
	        $("#updata").parentContainer[0].innerText="功能"
	        
	    }
	}
  	httpRequest.send(JSON.stringify(data)); //设置为发送给服务器数据
}



function clickCls(e)
{

    
	clsName=e.innerText.replace(/\s*/ig,"")
    
	
    data={"clsName":clsName}

	var httpRequest=new XMLHttpRequest()
    httpRequest.open("POST","/clickCls",true)
    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
    httpRequest.onreadystatechange = function () 
    {
	    if (httpRequest.readyState == 4 && httpRequest.status == 200)
	    {
	        flag=httpRequest.responseText
	        data=JSON.parse(flag)["musicName"]
	        showM=$("#showMore").parentContainer[0]
	        showM.style.color="#fff"
	        more=$("#more").parentContainer[0]
	        more.style.display="none"


	        nameContainer=document.getElementById("nameContainer")
	        nameContainer.value='{"all":'+JSON.stringify(data)+'}'
	        getSlice(0)

	         allLength=$("#allLength").parentContainer[0]
		     allLength.innerText="共"+Math.ceil(data.length/30)+"页"
		      $("#yesNumber").parentContainer[0].value=1

	    }
	}
  	httpRequest.send(JSON.stringify(data)); //设置为发送给服务器数据

}

function  change(e)
{
   pan=$("#change").parentContainer[0]
   fuc=$("#updata").parentContainer[0]
   fuc.innerText="关闭"

   pan.style.display="block"
   getData=$("#getData").parentContainer[0]
   changYes=$("#changYes").parentContainer[0]
   yes=$("#yes").parentContainer[0]
   subm=$("#subm").parentContainer[0]
   yes.style.display="none"
   subm.style.display="none"
   changYes.style.display="block"
   oldName=$("#oldName").parentContainer[0]
   musicName=e.name
   oldName.value=musicName
   changeValue=$(".changeValue").parentContainer
	var httpRequest=new XMLHttpRequest()
    httpRequest.open("POST","/change",true)
    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
    httpRequest.onreadystatechange = function () 
    {   
	    if (httpRequest.readyState == 4 && httpRequest.status == 200)
	    {
	        flag=httpRequest.responseText
	        data=JSON.parse(flag)

	        if(data["changeFlag"]=="getData")
	        {   
	        	changeValue[0].value=data["name"]
	        	changeValue[1].value=data["cls"]
	        	changeValue[2].value=data["pr"]
	        	getData.value="no"
	        }
	    }
	}

   if(getData.value=="yes")
   {

	 data={"changeFlag":"getData","musicName":musicName}
	 httpRequest.send(JSON.stringify(data)); //设置为发送给服务器数据
   }
   
  

}


function submData()
{
	pan=$("#change").parentContainer[0]
	oldName=$("#oldName").parentContainer[0].value
	changeValue=$(".changeValue").parentContainer
	name=changeValue[0].value
	cls=changeValue[1].value.replace(/\s*/ig,"")
	pr=changeValue[2].value.replace(/\s*/ig,"")


	var httpRequest=new XMLHttpRequest()
    httpRequest.open("POST","/change",true)
    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
    httpRequest.onreadystatechange = function () 
    {   
	    if (httpRequest.readyState == 4 && httpRequest.status == 200)
	    {
	        flag=httpRequest.responseText
	        data=JSON.parse(flag)
	        if(data["changeFlag"]=="ok")
	        {  
	        	showChangeBox()
	        	window.location.href="/music"
	        }
	    }
	}



	if(getData.value=="no")
   {
   	 data={"changeFlag":"no","oldName":oldName,"name":changeValue[0].value,"cls":changeValue[1].value,"pr":changeValue[2].value}
  	 httpRequest.send(JSON.stringify(data)); //设置为发送给服务器数据
   }

}


function getSlice(vs)
{
	nameContainer=document.getElementById("nameContainer")
	lenghName=nameContainer.value
	if(lenghName.length==0)
	{
		data={"index":"index"}
		var httpRequest=new XMLHttpRequest()
	    httpRequest.open("POST","/toMusic",true)
	    httpRequest.setRequestHeader("Content-Type", "application/json");   //设置请求头信息
	    httpRequest.onreadystatechange = function () 
	    {
		    if (httpRequest.readyState == 4 && httpRequest.status == 200)
		    {
		        flag=httpRequest.responseText
		        data=JSON.parse(flag)["all"]
		        nameContainer.value='{"all":'+JSON.stringify(data)+'}'
		        allLength=$("#allLength").parentContainer[0]
		        allLength.innerText="共"+Math.ceil(data.length/30)+"页"
		        nameSlice=data.slice(parseInt(vs)*30,(parseInt(vs)+1)*30)
				container=document.getElementById("showMusicTitle")
				container.innerHTML=""
				for(var i=0;i<nameSlice.length;i++)
				{   
					nameSpl=nameSlice[i].split(".")
					titleName=""
					l=nameSpl.length

					titleName=nameSpl.slice(0,l-1).join(".")
					
	       			 container.innerHTML+="<span class='sp'><span class='number'>"+(i*1+1)+"、</span><span onclick=cli(this) class='title'>"+titleName+"</span><button onclick=change(this) name='"+titleName+"'>修改</button></span>"
					
				}
		    }
		    
		}
	  	httpRequest.send(JSON.stringify(data)); //设
	}
	else
	{ 
		nameSlice=JSON.parse(lenghName)["all"].slice(parseInt(vs)*30,(parseInt(vs)+1)*30)
		
		container=document.getElementById("showMusicTitle")
	  	container.innerHTML=""
		for(var i=0;i<nameSlice.length;i++)
		{
			
			nameSpl=nameSlice[i].split(".")
			titleName=""
			l=nameSpl.length

			titleName=nameSpl.slice(0,l-1).join(".")		
   			container.innerHTML+="<span class='sp'><span class='number'>"+(i*1+1)+"、</span><span onclick=cli(this) class='title'>"+titleName+"</span><button onclick=change(this) name='"+titleName+"'>修改</button></span>"
		}
	}

}

function prev()
{
	yesNumber=$("#yesNumber").parentContainer[0]
	if(yesNumber.value==1)
	{
		alert("没有前一页")
	}
	else
	{ 
		getSlice(yesNumber.value*1-2)
		yesNumber.value=yesNumber.value*1-1
	}
}

function next()
{
	allLength=$("#allLength").parentContainer[0].innerText.replace("共","").replace("页","")
	count=parseInt(allLength)
	yesNumber=$("#yesNumber").parentContainer[0]
	if(count>yesNumber.value)
	{
		getSlice(yesNumber.value*1)
		yesNumber.value=yesNumber.value*1+1
	}
	else
	{
		alert("没有后一页")
	}
}

function toNumber()
{
	yesNumber=$("#yesNumber").parentContainer[0]
	allLength=$("#allLength").parentContainer[0]
	len=allLength.innerText.replace("共","").replace("页","")
	if(yesNumber.value>parseInt(len)||yesNumber.value<1)
	{
		alert("没有第"+yesNumber.value+"页")
	}
	else
	{
		getSlice(yesNumber.value*1-1)
		yesNumber.value=yesNumber.value*1
	}

}